using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class GameManager : MonoBehaviour
{
    public List<Bird> birds;
    public List<Pig> pigs;
    public static GameManager instance;
    private Vector3 originpos;
    public GameObject win;
    public GameObject lose;
    public GameObject PauseMnue;
    public GameObject[] stars;
    private int StarsNum=0;
    public int TotalLevel = 4;
    private void Start()
    {
        Initialized();
        instance = this;
        originpos = new Vector3(-0.833f, 0.29f,0);
        
    }
    public void Initialized()
    {
        for(int i = 0; i < birds.Count; i++)
        {
            if(i==0)
            {
                birds[i].enabled = true;
                birds[i].transform.position = originpos;
                birds[i].springJoint2D.enabled = true;
                birds[i].CanMove = true;
            }
            else
            {
                birds[i].enabled = false;
                birds[i].springJoint2D.enabled = false;
                birds[i].CanMove = false;
            }
        }
    }

    public void NextBird()
    {
        if(pigs.Count>0)
        {
            if(birds.Count>0)
            {
                Initialized();
            }
            else
            {
                PauseMnue.SetActive(false);
                lose.SetActive(true);
            }
        }
        else
        {
            PauseMnue.SetActive(false);
            win.SetActive(true);
        }
    }

    public void showStars()
    {
        StartCoroutine("showst");
    }

    IEnumerator showst()
    {
        for(; StarsNum < birds.Count; StarsNum++)
        {
            yield return new WaitForSeconds(0.3f);
            stars[StarsNum].SetActive(true);
        }
    }

    public void RePlay()
    {
        SaveData();
        SceneManager.LoadSceneAsync(1);
    }

    public void Home()
    {
        SaveData();
        SceneManager.LoadSceneAsync(0);
    }
    public void SaveData()
    {
        if(StarsNum>PlayerPrefs.GetInt(PlayerPrefs.GetString("nowLevel")))
        {
            PlayerPrefs.SetInt(PlayerPrefs.GetString("nowLevel"), StarsNum);
        }
        int num = 0;
        for(int i = 1; i < TotalLevel; i++)
        {
            num+=PlayerPrefs.GetInt("level"+i.ToString());
        }
        PlayerPrefs.SetInt("TotalNum", num);
    }
}
